package net.cloudsun.graph.puzzle.sql;

import net.cloudsun.graph.core.Edge;
import net.cloudsun.graph.puzzle.JigsawGraph;
import net.cloudsun.graph.puzzle.JigsawState;
import net.cloudsun.graph.puzzle.PuzzleEdge;
import net.cloudsun.graph.puzzle.PuzzlePosition;
import net.cloudsun.graph.util.FileUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Scanner;

/**
 * 将文件转换为SQL
 * created at 25/11/2021
 *
 * @author 俞建波
 * <a href="mailto://yujianbo@chtwm.com">yujianbo@chtwm.com</a>
 * @since 1.1.0
 */
public class FileToSql {

    public static void saveVertices(String originPath, String sqlDirName, String sqlFileName) throws IOException {
        final File file = FileUtils.initDirAndFile(sqlDirName, sqlFileName);
        try (final Scanner scanner = new Scanner(new File(originPath));
             final PrintStream printStream = new PrintStream(file, "UTF-8")
        ) {
            int id = 0;
            while (scanner.hasNext()) {
                final String line = scanner.nextLine();
                printStream.printf("insert into vertex(id, status) values(%d,'%s');\n", id++, line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void saveSmallGroups(String originPath, String sqlDirName, PuzzlePosition position) throws IOException {
        String sqlFileName = "small_rotate_groups.sql";
        final File file = FileUtils.initDirAndFile(sqlDirName, sqlFileName);

        try (final Scanner scanner = new Scanner(new File(originPath));
             final FileOutputStream fileOutputStream = new FileOutputStream(file, true);
             final PrintStream printStream = new PrintStream(fileOutputStream, true, StandardCharsets.UTF_8.toString())
        ) {
            int id = 0;
            while (scanner.hasNext()) {
                final String line = scanner.nextLine();
                final String code = line.substring(0, line.indexOf(" "));
                printStream.printf("insert into small_rotate_group(id, code, position) values(null,'%s', '%s');\n",
                        code, position.toString());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 本方法目的，从data中去除数据
     *
     * @param originPath 数据文件路径
     * @param position   位置
     * @param sqlDirName
     * @throws IOException
     */
    public static void updateSmallGroups(String originPath, String sqlDirName, int startId) throws IOException {
        String sqlFileName = "update_small_rotate_groups.sql";
        final File file = FileUtils.initDirAndFile(sqlDirName, sqlFileName);
        JigsawGraph jigsawGraph = new JigsawGraph(new File("jigsaw-vertices.dat"));
        try (final Scanner scanner = new Scanner(new File(originPath));
             final FileOutputStream fileOutputStream = new FileOutputStream(file, true);
             final PrintStream printStream = new PrintStream(fileOutputStream, true, StandardCharsets.UTF_8.toString())
        ) {

            while (scanner.hasNext()) {
                final String line = scanner.nextLine();
                final int endIndex = line.indexOf(" ");
                final String code = line.substring(0, endIndex);
                final String groups = line.substring(endIndex + 1);
                String[] groupArray = groups.split("\\s");

                printStream.print("update small_rotate_group set ");
                for (int i = 0; i < groupArray.length; i++) {
                    final String s = groupArray[i];
                    if (i == 0) {
                        printStream.printf("e = %d", jigsawGraph.getStateIndexMap().get(new JigsawState(s)));
                    } else {
                        printStream.printf(", r%d = %d",i, jigsawGraph.getStateIndexMap().get(new JigsawState(s)));
                    }
                }
                printStream.printf(" where id = %d;\n", startId++);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void saveEdges(String source, String sqlDirName, String sqlFileName) throws IOException {
        JigsawGraph jigsawGraph = new JigsawGraph(new File(source));
        final File file = FileUtils.initDirAndFile(sqlDirName, sqlFileName);
        try (
                final FileOutputStream fileOutputStream = new FileOutputStream(file, true);
                final PrintStream printStream = new PrintStream(fileOutputStream, true, StandardCharsets.UTF_8.toString())
        ) {
            int id = 0;
            final List<? extends Edge>[] edges = jigsawGraph.getEdges();
            for (List<? extends Edge> edgeList : edges) {
                for (Edge edge : edgeList) {
                    if (edge instanceof PuzzleEdge) {
                        PuzzleEdge puzzleEdge = (PuzzleEdge) edge;
                        printStream.printf("insert into edge(id, from_vertex, to_vertex, direction) values(null,%d, %d, '%s');\n",
                                edge.getFrom(), edge.getTo(), puzzleEdge.getDirection());
                    }
                }

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
